package xcmg.device.web.agentReturn;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.agentReturn.AgentReturnService;
import xcmg.device.service.vo.agentReturn.AgentReturnAddVO;
import xcmg.device.service.vo.agentReturn.AgentReturnAuditVO;
import xcmg.device.service.vo.agentReturn.AgentReturnModifyVO;
import xcmg.device.service.vo.agentReturn.AgentReturnQueryVO;
import xcmg.device.service.vo.delivery.ManualFinishVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;
import java.util.List;

/**
 * @author: xiali@bkrwin.com
 * @description: 供应商退货单Controller
 * @date： 2019/6/18 20:16
 **/
@RestController
@RequestMapping("/agentReturn")
public class AgentReturnController extends BasicController {
    @Autowired
    AgentReturnService agentReturnService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "新增供应商退货单")
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody AgentReturnAddVO agentReturnAddVO) throws Exception {
        if (agentReturnAddVO == null) {
            return actionResult(ErrorCode.IllegalArument);
        }
        agentReturnAddVO.setOrgId(getOrgId());
        ErrorCode ret = agentReturnService.add(agentReturnAddVO, getUserId(), getUserName());
        return actionResult(ret);
    }


    @RequestMapping(value = "/modify", method = RequestMethod.POST)
    @ApiOperation(value = "修改供应商退货单")
    @FastMappingInfo(needLogin = true)
    public ActionResult modify(@RequestBody AgentReturnModifyVO agentReturnModifyVO) throws Exception {
        if (agentReturnModifyVO == null) {
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode ret = agentReturnService.modify(agentReturnModifyVO, getUserId(), getUserName());
        return actionResult(ret);
    }


    @RequestMapping(value = "/audit", method = RequestMethod.POST)
    @ApiOperation(value = "审核供应商退货记录")
    @FastMappingInfo(needLogin = true)
    public ActionResult audit(@RequestBody AgentReturnAuditVO agentReturnAuditVO) throws Exception {
        if (null == agentReturnAuditVO) {
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode ret = agentReturnService.audit(agentReturnAuditVO, getUserId(), getUserName(), getOrgId());
        return actionResult(ret);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "供应商退货列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<AgentReturnQueryVO> condition) throws Exception {
        AgentReturnQueryVO agentReturnQueryVO = condition.getFilters();
        if (null == agentReturnQueryVO) {
            agentReturnQueryVO = new AgentReturnQueryVO();
        }
        agentReturnQueryVO.setOrgId(getOrgId());
        condition.setFilters(agentReturnQueryVO);
        PageCommonVO pageCommonVO = agentReturnService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/discardBatch", method = RequestMethod.POST)
    @ApiOperation(value = "删除供应商退货记录")
    @FastMappingInfo(needLogin = true)
    public ActionResult discardBatch(@RequestBody List<String> ids) throws Exception {
        if (ids == null || ids.size() == 0) {
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionResult(agentReturnService.discardBatch(ids, getUserId(), getUserName(),getOrgId()));
    }

    @PostMapping(value = "/manualFinish")
    @ApiOperation("手动完成退货单")
    @FastMappingInfo(needLogin = true)
    public ActionResult manualFinish(@RequestBody ManualFinishVO manualFinishVO) throws Exception {
        return actionResult(agentReturnService.manualFinish(manualFinishVO.getBillNo(), manualFinishVO.getMaterialsNo(), getOrgId()));
    }

    @PostMapping(value = "/inbound")
    @ApiOperation("入库")
    @FastMappingInfo(needLogin = true)
    public ActionResult inbound(@RequestBody String id) throws Exception {
        return actionResult(agentReturnService.inbound(id));
    }

    @PostMapping(value = "/export")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("根据勾选数据导出")
    public void export(String[] ids) throws Exception {
        agentReturnService.export(Arrays.asList(ids));
    }

    @PostMapping(value = "/exportAll")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("导出所有数据")
    public void exportAll(AgentReturnQueryVO condition) throws Exception {
        if (null == condition) {
            condition = new AgentReturnQueryVO();
        }
        condition.setOrgId(getOrgId());
        agentReturnService.exportAll(condition);
    }

    @GetMapping(value = "getTheAmountOwed")
    @ApiOperation(value = "获取欠款金额")
    @FastMappingInfo(needLogin = true)
    public ActionResult getTheAmountOwed(@RequestParam("returnId") String returnId) throws Exception {
        return actionValueResult(agentReturnService.getTheAmountOwed(returnId, getOrgId()));
    }
}
